//
//  YBCycleView.m
//  YBCycleView
//
//  Created by 陈亚勃 on 15/12/29.
//  Copyright © 2015年 cyb. All rights reserved.
//

#import "YBCycleView.h"

@interface YBCycleView()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *cycleView;

@property (nonatomic, strong) NSMutableArray *cycleImageViews;

@property (nonatomic, strong) NSArray *cycleImages;

@property (nonatomic, assign) NSInteger selectedIndex;

@property (nonatomic, assign) CGFloat scrollViewX;

@property (nonatomic, assign) CGFloat scrollViewY;

@property (nonatomic, assign) CGFloat scrollViewWidth;

@property (nonatomic, assign) CGFloat scrollViewHeiht;

@property (nonatomic, strong) NSMutableArray *pageControls;

@end

@implementation YBCycleView

- (NSMutableArray *)cycleImageViews{
    if (_cycleImageViews == nil) {
        _cycleImageViews = [NSMutableArray array];
    }
    return _cycleImageViews;
}

- (NSMutableArray *)pageControls{
    if (_pageControls == nil) {
        _pageControls = [NSMutableArray array];
    }
    return _pageControls;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _scrollViewX = frame.origin.x;
        _scrollViewY = frame.origin.y;
        _scrollViewWidth = frame.size.width;
        _scrollViewHeiht = frame.size.height;
        
        _cycleView = [[UIScrollView alloc] initWithFrame:frame];
        _cycleView.bounces = NO;
        _cycleView.delegate = self;
        _cycleView.pagingEnabled = YES;
        _cycleView.showsHorizontalScrollIndicator = _cycleView.showsVerticalScrollIndicator = NO;
        
        _selectedIndex = 1;
        [self addSubview:_cycleView];
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray<UIImage *> *)images withAnimationTimer:(NSTimeInterval)time{
   
    self = [self initWithFrame:frame];
   
    _cycleImages = images;
    
    CGRect imageRect = frame;
    
    for (int i = 0; i < 3; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:imageRect];
        image.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
        [image addGestureRecognizer:tapGR];
        [self.cycleView addSubview:image];
        imageRect.origin.x += imageRect.size.width;
        _cycleView.contentSize = CGSizeMake(imageRect.origin.x, 0);
        [self.cycleImageViews addObject:image];
    }


    self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(animation:) userInfo:nil repeats:YES];
    [self.animationTimer fire];
    
    return self;
}

/**
 *  自己定时滚动方法
 *
 *  @param timer timer
 */
- (void)animation:(NSTimer *)timer{
    [_cycleView setContentOffset:CGPointMake(_cycleView.contentOffset.x + _scrollViewWidth, 0) animated:YES];
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    [self scroll];
    
    NSInteger currentindex = _selectedIndex;
    for (UIButton *btn in self.pageControls) {
        if (btn.tag  == currentindex) {  //选择样式
            [btn setImage:[UIImage imageNamed:@"banner_yuan01_"] forState:UIControlStateNormal];
        }else{      //未选中样式
            [btn setImage:[UIImage imageNamed:@"banner_yuan02_"] forState:UIControlStateNormal];
        }
    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scroll];
}

- (NSInteger)countIndex:(NSInteger) index{
    
    if (index < 0) {
        index = _cycleImages.count - 1;
    }
    
    if (index > _cycleImages.count - 1) {
        index = 0;
    }
    
    return index;
}

- (void)scroll{
    //第一张
    if (_cycleView.contentOffset.x == 0) {
        [_cycleView setContentOffset:CGPointMake(_scrollViewWidth, 0) animated:NO];
        _selectedIndex--;
    }
    //最后一张
    if (_cycleView.contentOffset.x == _scrollViewWidth * 2) {
        [_cycleView setContentOffset:CGPointMake(_scrollViewWidth, 0) animated:NO];
        _selectedIndex++;
    }
    
    _selectedIndex = [self countIndex:_selectedIndex];
    NSInteger lastIndex = [self countIndex:_selectedIndex - 1];
    NSInteger nextIndex = [self countIndex:_selectedIndex + 1];
    
    [_cycleImageViews[0] setImage:_cycleImages[lastIndex]];
    [_cycleImageViews[1] setImage:_cycleImages[_selectedIndex]];
    [_cycleImageViews[2] setImage: _cycleImages[nextIndex]];
}

- (void)tapImageAction:(UITapGestureRecognizer *)tapGR{
    NSLog(@"%ld",_selectedIndex);
    if (self.tapCycleViewBlock) {
        self.tapCycleViewBlock(_selectedIndex - 1);
    }
}

@end
